package com.example.usagelock;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;

public class Navigator extends Label{
	
	Paint backgroundPaint;
	Bitmap background;
	Rect touchCollisionRect;
	Rect drawRect;
	final int radius = 60;
	
	PasswordOrganizer po;
	Queue<Integer> queueForPasswords;
	Queue<Integer> passwordsPutByUser;
	
	Vibrator v;
	boolean[] vibratorBool;
	
	public Navigator(Context context,  int numberOfIcons, int left, int top, int right, int bottom) {
		super(context, left, top, right, bottom);
		
		b = new ArrayList<Bitmap>();
		
		v = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
		
		po = new PasswordOrganizer(context);
		
		queueForPasswords = new LinkedList<Integer>();
		passwordsPutByUser = new LinkedList<Integer>();
		
		for(int i=0; i<po.getLength(); i++){
			queueForPasswords.add(po.getPassword(i));
		}
		
		vibratorBool = new boolean[numberOfIcons];
		for(int i=0; i<vibratorBool.length; i++){
			vibratorBool[i] = true;
		}		
		
		
		touchCollisionRect = new Rect(centerX - radius, centerY - radius, 
				centerX + radius, centerY + radius);
		drawRect = new Rect();
	}
	
	int draw(Canvas canvas, PointF point, ArrayList<Label> labelSet, int count){
		canvas.drawColor(0, PorterDuff.Mode.CLEAR);
		
		if(count==0){			
			canvas.drawBitmap(b.get(0), null, r, paint);
		}
		
		if(point!=null){
			if((count==0) && touchCollisionRect.contains((int)point.x, (int)point.y)){
				count++;
			}else if(count>0){
				drawRect.set((int)point.x-radius, (int)point.y-radius, 
						(int)point.x+radius, (int)point.y+radius);
				canvas.drawBitmap(b.get(1), null, drawRect, paint);

				for(int i=0; i<labelSet.size(); i++){
					labelSet.get(i).draw = true;
				}				
				
				for(int i=0; i<labelSet.size(); i++){
					count = navigatorDockingToIcon(canvas, point, labelSet.get(i), i, count);
				}
			}
		}
		return count;
	}	
	
	int navigatorDockingToIcon(Canvas canvas, PointF point, Label label, int index, int count){
		if((((label.centerX-90)<=point.x) && ((label.centerX+90)>=point.x)) && 
				(((label.centerY-90)<=point.y) && ((label.centerY+90)>=point.y))){
			canvas.drawColor(0, PorterDuff.Mode.CLEAR);
			label.drawBitmap(canvas, 2);
			label.draw = false;
			if(vibratorBool[index]==true){
				v.vibrate(60);
				
				passwordsPutByUser.add(label.getImageType());
				
				vibratorBool[index] = false;
				for(int i=0; i<vibratorBool.length; i++){
					if(i==index){
						continue;
					}
					vibratorBool[i] = true;
				}
				
				if(label.getImageType() == ImageType.Phone){
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:"));
					context.startActivity(intent);
				}else if(label.getImageType() == ImageType.Config){
					Intent intent = new Intent(context, ConfigActivity.class);
					context.startActivity(intent);
				}
			}
			count++;			
		}
		else{
			vibratorBool[index] = true;
		}
		
		return count;
	}
	
	void resetQueueForUser(){
		passwordsPutByUser.clear();
	}
	
	void resetQueueForPasswords(){
		queueForPasswords.clear();
		for(int i = 0; i < po.getLength(); i++){
			queueForPasswords.add(po.getPassword(i));
		}
	}
	
	boolean checkPasswords(){
		boolean result = true;
		resetQueueForPasswords();
		Iterator<Integer> passwordIterator = queueForPasswords.iterator();
		Iterator<Integer> userIterator = passwordsPutByUser.iterator();
		
		while(passwordIterator.hasNext() || userIterator.hasNext()){
			int password = 0;
			int user = 0;
			try{
				password = passwordIterator.next();
				user = userIterator.next();
			}catch(NoSuchElementException e){
				result = false;
				break;
			}
			
			if(user != password){	
				result = false;
				break;
			}
		}
		
		resetQueueForUser();
		resetQueueForPasswords();
		return result;
	}	
}